package com.csproj.dispatch.resource;

import com.codahale.metrics.annotation.Timed;
import com.csproj.dispatch.service.LogisticsService;
import com.csproj.dispatch.service.dto.LogisticsDTO;
import com.csproj.siiat.base.common.dto.BaseDTO;
import com.csproj.system.domain.UserInfo;
import com.csproj.system.service.SysLogsService;
import com.csproj.system.service.UserInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * REST controller for managing Logistics.
 * 物流信息Controller
 */
@RestController
@RequestMapping("/api/center/ltr")
public class LogisticsResource {

    private final Logger log = LoggerFactory.getLogger(LogisticsResource.class);

    private static final String ENTITY_NAME = "logistics";

    private final LogisticsService logisticsService;
    private final UserInfoService userInfoService;
    private final SysLogsService sysLogsService;
    public LogisticsResource(LogisticsService logisticsService,UserInfoService userInfoService,SysLogsService sysLogsService) {
        this.logisticsService = logisticsService;
        this.userInfoService = userInfoService;
        this.sysLogsService = sysLogsService;
    }

    /**
     * 创建物流信息
     * @param request
     * @param logisticsDTO
     * @return
     */
    @PostMapping("/logisticsInfos")
    @Timed
    public BaseDTO insertLogistics(HttpServletRequest request, @RequestBody LogisticsDTO logisticsDTO){
        LogisticsDTO logisticsDTO1 =  logisticsService.save(logisticsDTO);

        UserInfo userInfo = userInfoService.getCurrentUser();
        sysLogsService.saveForGet("", userInfo.getName()+":user_create_logistics","");
        return BaseDTO.createBaseDTO(logisticsDTO1);
    }

    /**
     * 通过集装箱Id获取物流信息
     * @param containerid 集装箱信息主键
     * @return
     */
    @GetMapping("/logisticsInfos/{containerid}")
    @Timed
    public BaseDTO searchLogistics(@PathVariable String containerid){
        LogisticsDTO logisticsDTO = logisticsService.findLogisticsByContainerid(containerid);
        UserInfo userInfo = userInfoService.getCurrentUser();
        sysLogsService.saveForGet("", userInfo.getName()+":user_view_logistics","");
        return BaseDTO.createBaseDTO(logisticsDTO);
    }

    /**
     * 通过物流信息id更新物流信息
     * @param logisticsDTO 物流信息
     * @param id 物流信息id
     * @return
     */
    @PutMapping("/logisticsInfos/{id}")
    @Timed
    public BaseDTO updateLogistics(@RequestBody LogisticsDTO logisticsDTO, @PathVariable String id){
        LogisticsDTO logisticsDTO1 = logisticsService.updateLogisticsById(id, logisticsDTO);

        UserInfo userInfo = userInfoService.getCurrentUser();
        sysLogsService.saveForGet("", userInfo.getName()+":user_update_logistics","");
        return BaseDTO.createBaseDTO(logisticsDTO1);
    }

    /**
     * 通过主键删除物流信息
     * @param id 物流id
     * @return
     */
    @DeleteMapping("/logisticsInfos/{id}")
    @Timed
    public BaseDTO deleteLogistics(@PathVariable String id){
        return logisticsService.deleteLogisticsById(id);
    }


}